﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using Hp.Vertica.Migration.SqlServer;

namespace Hp.Vertica.Client
{
    public class SqlConnectionViewModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        private String _dataSource;
        private Boolean _integratedSecurity;
        private String _user;
        private String _password;

        private Boolean _isConnecting = false;

        public String DataSource
        {
            get { return this._dataSource; }
            set
            {
                if (value != _dataSource)
                {
                    _dataSource = value;
                    this.OnPropertyChanged("DataSource");
                }
            }
        }

        public Boolean IntegratedSecurity
        {
            get { return _integratedSecurity; }
            set
            {
                if (value != _integratedSecurity)
                {
                    _integratedSecurity = value;
                    this.OnPropertyChanged("IntegratedSecurity");
                    this.OnPropertyChanged("NotIntegratedSecurity");
                }
            }
        }

        public String User
        {
            get { return _user; }
            set
            {
                if (value != _user)
                {
                    _user = value;
                    this.OnPropertyChanged("User");
                }
            }
        }

        public String Password
        {
            get { return _password; }
            set
            {
                if (value != _password)
                {
                    _password = value;
                    this.OnPropertyChanged("Password");
                }
            }
        }

        public Boolean IsConnecting
        {
            get { return _isConnecting; }
            set
            {
                if (value != _isConnecting)
                {
                    _isConnecting = value;
                    this.OnPropertyChanged("IsConnecting");
                    this.OnPropertyChanged("IsEditing");
                }
            }
        }

        public ServerInstance Save()
        {
            ServerInstance model = this.IntegratedSecurity
                ? (new ServerInstance(this.DataSource))
                : (new ServerInstance(this.DataSource, this.User, this.Password));
            model.Connect();
            return model;
        }

        protected virtual void OnPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
